package com.bjpowernode.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


//1——request请求对象
//2——response响应对象
//3——Session会话，服务器数据
//4——Cookies姜饼，浏览器数据（客户端数据）【前提是浏览器要开cookies】
@Controller
public class HttpController {

    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    HttpServletResponse httpServletResponse;

    @RequestMapping("/http")
    public String fun1() {
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("hzj", 9000);
        return "http";
    }

    @RequestMapping("/http-cookies")
    public String fun2() {
        Cookie cookieA = new Cookie("a", "111");
        cookieA.setMaxAge(5 * 60);
        cookieA.setPath("/");

        Cookie cookieB = new Cookie("b", "222");
        cookieB.setMaxAge(5 * 60);
        cookieB.setPath("/");

        Cookie cookieC = new Cookie("c", "333");
        cookieC.setMaxAge(5 * 60);
        cookieC.setPath("/");

        httpServletResponse.addCookie(cookieA);
        httpServletResponse.addCookie(cookieB);
        httpServletResponse.addCookie(cookieC);


        return "http";
    }

//    @RequestMapping("/http")
//    public String fun2(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
//        return "http";
//    }

    @RequestMapping("/http-get-cookie")
    public String fun3(Model model) {
        Cookie[] cookies = httpServletRequest.getCookies();
        model.addAttribute("cookies",cookies);
        model.addAttribute("hzj","cookies");
        httpServletRequest.setAttribute("Request","setAttribute");
        return "http";
    }


}
